<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: create by allen.qiang
// +----------------------------------------------------------------------

/**
 * 内容管理操作类 articleCategory
 * @package  htdocs
 * @subpackage  module/base
 * @author   create by allen.qiang
 * @version 1.0
 */

class contentArticle {

    /**
     * 查询文章
     * @param array $arg_array 查询参数数组
     * @return null
     */
    public function searchArticle( &$arg_array ) {

        global $G_SHOP;

        //sql字符串
        $sql = '';

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();


        //文章ID
        if (isset($arg_array['article_id']) && is_numeric($arg_array['article_id'])) {
            $sql .= ' AND article_id = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['article_id'];
        }

        //文章标题
        if (isset($arg_array['article_title']) && !empty($arg_array['article_title'])) {
            $sql .= ' AND article_title LIKE ?';
            $params_type .= 's';
            $params_array[] = '%' . $arg_array['article_title'] . '%';
        }

        //使用标志
        if (isset($arg_array['useFlg']) && is_numeric($arg_array['useFlg'])) {
            $sql .= ' AND useFlg = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['useFlg'];
        }

        //文章外部链接
        if (isset($arg_array['article_link']) && is_numeric($arg_array['article_link'])) {
            $sql .= ' AND article_link = ?';
            $params_type .= 's';
            $params_array[] = $arg_array['article_link'];
        }

        //文章摘要
        if (isset($arg_array['article_summary']) && is_numeric($arg_array['article_summary'])) {
            $sql .= ' AND article_summary = ?';
            $params_type .= 's';
            $params_array[] = $arg_array['article_summary'];
        }

        //描述
        if (isset($arg_array['description']) && is_numeric($arg_array['description'])) {
            $sql .= ' AND description = ?';
            $params_type .= 's';
            $params_array[] = $arg_array['description'];
        }

        //文章分类索引
        if (isset($arg_array['article_category_index']) && !empty($arg_array['article_category_index'])) {
            $sql .= ' AND article_category_index = ?';
            $params_type .= 's';
            $params_array[] = $arg_array['article_category_index'];
        }

        //分类ID
        if (isset($arg_array['article_category_id']) && is_numeric($arg_array['article_category_id'])) {
            $sql .= ' AND article_category_id = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['article_category_id'];
        }


        //文章分类名称
        if (isset($arg_array['article_category_name']) && !empty($arg_array['article_category_name'])) {
            $sql .= ' AND article_category_name = ?';
            $params_type .= 's';
            $params_array[] = $arg_array['article_category_name'];
        }

        //文章类型
        if (isset($arg_array['article_type']) && is_numeric($arg_array['article_type'])) {
            $sql .= ' AND article_type = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['article_type'];
        }

        //新闻标题图片
        if (isset($arg_array['article_pic']) && is_numeric($arg_array['article_pic'])) {
            $sql .= ' AND article_pic = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['article_pic'];
        }

        if (isset($arg_array['extend']) && !empty($arg_array['extend'])) {
            $sql .= $arg_array['extend'];
        }

        if ($sql) {
            $sql = 'WHERE ' . substr($sql, 4);
        }

        if (isset($arg_array['rp'])) {

            //统计数量
            $search_sql = 'SELECT COUNT(*) AS item_count FROM content_article ' . $sql;
            $count_array = &$G_SHOP->DBCA->stmt_query($search_sql, $params_type, $params_array);
            $item_count = $count_array[0]->item_count;
            if ($item_count == 0) {
                $arg_array['count'] = 0;
                return null;
            }

            $arg_array['count'] = $item_count;

            //排序
            if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

            //分页
            if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
                $arg_array['page'] = 1;

            $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);

        } else {

            //排序
            if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

        }

        //查询分类信息
        $search_sql = 'SELECT * FROM content_article ' . $sql;
        $temp_item_array = &$G_SHOP->DBCA->stmt_query($search_sql, $params_type, $params_array);
        if (!$temp_item_array)
            return null;

        $item_array = array();
        foreach($temp_item_array as &$item) {
            $item_array[] = array(
                'article_id' => $item->article_id,
                'article_title' => $item->article_title,
                'useFlg' => $item->useFlg,
                'commentFlg' => $item->commentFlg,
                'article_pic' => $item->article_pic,
                'article_pic_show' => G_IMAGE_URL . $item->article_pic,
                'article_link' => $item->article_link,
                'article_summary' => $item->article_summary,
                'description' => isset($arg_array['article_id']) ? $item->description : null,
                'article_category_index' => $item->article_category_index,
                'article_category_id' => $item->article_category_id,
                'article_category_name' => $item->article_category_name,
                'article_type' => $item->article_type,
                'article_date' => $item->article_date,
                'createTime' => $item->createTime,
                'creator' => $item->creator,
                'article_link' => $item->article_link,
                'page_link' => $item->article_link ? $item->article_link : G_WEBSITE_DOMAINNAME . sprintf('/shop/article-%d.html', $item->article_id)
            );

        }

        return $item_array;

    }

    /**
     * 添加或修改文章信息
     *
     * @param array $arg_array 文章信息数组
     *
     */
    function saveArticleInfo ($arg_array) {

        global $G_SHOP;

        quanli_include('module/base/common/quanliFile.class.php');

        $quanliFile = new quanliFile();

        //检查是否缺少参数
        if (!isset( $arg_array['article_id'], $arg_array['article_title'], $arg_array['category_id'], $arg_array['article_link'], $arg_array['article_summary'], $arg_array['description'],$arg_array['article_pic']))
            return array( 'code' => 400, 'message' => '缺少参数' );

        //检查标题
        if (empty($arg_array['article_title']))
            return array( 'code' => 400, 'message' => '请填写文章标题' );

        //检查外部链接、正文
        if ( empty($arg_array['article_link']) && empty($arg_array['description']) )
            return array( 'code' => 400, 'message' => '请填写外部链接或者正文内容' );

        //检查分类
        if (!is_numeric($arg_array['category_id']) || $arg_array['category_id'] <= 0)
            return array( 'code' => 400, 'message' => '请选择文章分类' );

        //检查文章ID
        if (empty($arg_array['article_id']) || !is_numeric($arg_array['article_id']) || $arg_array['article_id'] < 0)
            $arg_array['article_id'] = 0;

        $arg_array['article_title'] = convertVar($arg_array['article_title']);
        $arg_array['article_link'] = convertVar($arg_array['article_link']);
        $arg_array['article_summary'] = convertVar($arg_array['article_summary']);
        $arg_array['description'] = convertVar($arg_array['description']);
        $arg_array['article_pic'] = convertVar($arg_array['article_pic']);

        try {

            //生成新的图片地址
            if($arg_array['article_pic']){
                $new_file_address = $quanliFile->basename_db($arg_array['article_pic']);
            }

            //查询文章分类信息
            $c_arg_array = array(
                'category_id' => $arg_array['category_id']
            );
            $article_category_array = $this->searchArticleCategory( $c_arg_array );
            if (!$article_category_array)
                return array( 'code' => 404, 'message' => '文章分类信息没有找到' );

            //添加或修改文章信息
            $article_obj = new entity('content_article');
            if ($arg_array['article_id'] > 0) {
                $article_obj->article_id = $arg_array['article_id'];
                if (!$G_SHOP->DBCA->fetch($article_obj))
                    return array( 'code' => 404, 'message' => '文章信息没有找到' );
                $article_obj->editor = $arg_array['creator'];
            } else {
                $article_obj->creator = $arg_array['creator'];
                $article_obj->article_date = date('Y-m-d');
            }
            $article_obj->article_title = $arg_array['article_title'];

            if (isset($arg_array['commentFlg'])) {
                $article_obj->commentFlg = $arg_array['commentFlg'];
            }

            if (isset($arg_array['article_pic'])) {
                $article_obj->article_pic = $arg_array['article_pic'];
            }

            $article_obj->article_pic = $arg_array['article_pic'];
            if($arg_array['article_pic']){
                $article_obj->article_pic = $new_file_address;
            }
            $article_obj->article_summary = $arg_array['article_summary'];

            $article_obj->article_category_id = $article_category_array[0]['category_id'];
            $article_obj->article_category_index = $article_category_array[0]['category_index'];
            $article_obj->article_category_name = $article_category_array[0]['category_title'];
            $article_obj->article_type = $article_category_array[0]['category_type'];
            $article_obj->description = $arg_array['description'];
            $article_obj->article_link = $arg_array['article_link'];
            $G_SHOP->DBCA->save($article_obj);

            if ($arg_array['article_id'] > 0 ) {
                return array('code' => 200 , 'message' => '文章修改成功');
            } else {
                return array('code' => 200 , 'message' => '文章保存成功');
            }

        } catch (Exception $e){
            error_log($e->getMessage());
            return array('code' => 500, 'message' => '系统错误');
        }
    }

    /**
     * 删除文章信息
     *
     * @param array $arg_array
     * @return array
     */
    public function deleteArticle( $arg_array ) {

        global $G_SHOP;

        if (!isset($arg_array['delete_str']))
            return array( 'code' => 400, 'message' => '缺少参数' );

        if (empty($arg_array['delete_str']))
            return array( 'code' => 400, 'message' => '请选择要删除的资源位' );

        $delete_array = explode('_', $arg_array['delete_str']);

        try {

            foreach($delete_array as &$article_id) {

                if (!is_numeric($article_id))
                    return array( 'code' => 400, 'message' => '参数错误' );

                //查询资源位信息
                $article_obj = new entity('content_article');
                $article_obj->article_id = $article_id;
                if ( $G_SHOP->DBCA->fetch($article_obj)) {
                    $G_SHOP->DBCA->delete($article_obj);
                }

            }

            return array( 'code' => 200, 'message' => '删除成功' );

        } catch (Exception $e) {

            error_log($e->getMessage());
            return array( 'code' => 500, 'message' => '系统错误' );

        }

    }


    /**
     * 查询文章分类
     * @param array $arg_array 查询参数数组
     * @return null
     */
    public function searchArticleCategory( &$arg_array ) {

        global $G_SHOP;

        //sql字符串
        $sql = '';

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();

        //文章分类ID
        if (isset($arg_array['category_id']) && quanli_is_int($arg_array['category_id'])) {
            $sql .= ' AND category_id = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['category_id'];
        }

        //文章分类索引
        if (isset($arg_array['category_index']) && !empty($arg_array['category_index'])) {
            $sql .= ' AND category_index = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['category_index'];
        }

        //文章分类标题
        if (isset($arg_array['category_title']) && !empty($arg_array['category_title'])) {
            $sql .= ' AND category_title LIKE ?';
            $params_type .= 's';
            $params_array[] = '%' . $arg_array['category_title'] . '%';
        }

        //文章分类类型
        if (isset($arg_array['category_type']) && !empty($arg_array['category_type'])) {
            $sql .= ' AND category_type = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['category_type'];
        }

        //描述
        if (isset($arg_array['description']) && !empty($arg_array['description'])) {
            $sql .= ' AND description = ?';
            $params_type .= 's';
            $params_array[] = $arg_array['description'];
        }

        //使用标志
        if (isset($arg_array['useFlg']) && in_array($arg_array['useFlg'], array(0, 1))) {
            $sql .= ' AND useFlg = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['useFlg'];
        }

        if ($sql) {
            $sql = 'WHERE ' . substr($sql, 4);
        }

        if (isset($arg_array['rp']) && $arg_array['rp'] > 1) {

            //统计数量
            $search_sql = 'SELECT COUNT(*) AS item_count FROM content_article_category ' . $sql;
            $count_array = &$G_SHOP->DBCA->stmt_query($search_sql, $params_type, $params_array);
            $item_count = $count_array[0]->item_count;
            if ($item_count == 0) {
                $arg_array['count'] = 0;
                return null;
            }

            $arg_array['count'] = $item_count;

            //排序
            if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

            //分页
            if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
                $arg_array['page'] = 1;

            $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);

        } else {

            //排序
            if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

        }

        //查询分类信息
        $search_sql = 'SELECT * FROM content_article_category ' . $sql;
        $temp_item_array = &$G_SHOP->DBCA->stmt_query($search_sql, $params_type, $params_array);
        if (!$temp_item_array)
            return null;

        $item_array = array();
        foreach($temp_item_array as &$item) {
            $item_array[] = array(
                'category_id' => $item->category_id,
                'category_index' => $item->category_index,
                'category_title' => $item->category_title,
                'description' => $item->description,
                'useFlg' => $item->useFlg,
                'category_type' => $item->category_type
            );

        }

        return $item_array;

    }

    /**
     * 添加或修改文章分类信息
     * @param array $arg_array 文章分类信息
     * @return array
     */
    public function addArticleCategory( $arg_array ) {

        global $G_SHOP;

        //检查是否缺少参数
        if (!isset($arg_array['category_id'], $arg_array['category_index'], $arg_array['category_title'], $arg_array['category_type'], $arg_array['description']))
            return array( 'code' => 400, 'message' => '缺少参数' );
        //检查分类ID
        if (!is_numeric($arg_array['category_id']))
            return array( 'code' => 400, 'message' => '参数错误' );
        //检查分类别名
        if (empty($arg_array['category_index']))
            return array( 'code' => 400, 'message' => '分类别名不能为空' );
        //检查分类标题
        if (empty($arg_array['category_title']))
            return array( 'code' => 400, 'message' => '分类标题不能为空' );
        //检查分类
        if (!is_numeric($arg_array['category_type']) || $arg_array['category_type'] <= 0)
            return array( 'code' => 400, 'message' => '请选择分类类型' );


        try {

            $article_obj = new entity('content_article_category');
            if ($arg_array['category_id'] > 0) {
                $article_obj->category_id = $arg_array['category_id'];
                if (!$G_SHOP->DBCA->fetch($article_obj))
                    return array( 'code' => 404, 'message' => '文章分类信息没有找到' );
                $article_obj->editor = $arg_array['creator'];
            } else {
                $article_obj->creator = $arg_array['creator'];
            }
            $article_obj->category_id = $arg_array['category_id'];
            $article_obj->category_index = $arg_array['category_index'];
            $article_obj->category_title = $arg_array['category_title'];
            $article_obj->category_type = $arg_array['category_type'];
            $article_obj->description = $arg_array['description'];
            $article_obj->useFlg = $arg_array['useFlg'];


            $G_SHOP->DBCA->save($article_obj);

            if ($arg_array['category_id'] == 0)
                return array( 'code' => 200, 'message' => '保存成功' );
            else
                return array( 'code' => 200, 'message' => '修改成功' );

        } catch (Exception $e) {

            error_log($e->getMessage());
            return array( 'code' => 500, 'message' => '系统错误' );

        }

    }

    /**
     * 删除文章分类信息
     *
     * @param array $arg_array
     * @return array
     */
    public function deleteArticleCategory( $arg_array ) {

        global $G_SHOP;

        if (!isset($arg_array['delete_str']))
            return array( 'code' => 400, 'message' => '缺少参数' );

        if (empty($arg_array['delete_str']))
            return array( 'code' => 400, 'message' => '请选择要删除的文章分类' );

        $delete_array = explode('_', $arg_array['delete_str']);

        try {

            foreach($delete_array as &$article_category_id) {

                if (!is_numeric($article_category_id))
                    return array( 'code' => 400, 'message' => '参数错误' );

                //删除文章分类
                $article_obj = new entity('content_article_category');
                $article_obj->category_id = $article_category_id;
                if ($G_SHOP->DBCA->fetch($article_obj)) {
                    $G_SHOP->DBCA->delete($article_obj);

                    //删除分类下所有文章
                    $del_sql = 'DELETE FROM content_article WHERE article_category_id = ' . $article_category_id;
                    $G_SHOP->DBCA->execute($del_sql);

                }

            }

            return array( 'code' => 200, 'message' => '删除成功' );

        } catch (Exception $e) {

            error_log($e->getMessage());
            return array( 'code' => 500, 'message' => '系统错误' );

        }

    }

    /**
    * 文章启用/禁用
    * @param $arg_array
    * @return array
    */
    public function changeArticleUseflg ( $arg_array ) {

        global $G_SHOP;

        if (!isset($arg_array['article_id'], $arg_array['flg']) || !quanli_is_int($arg_array['article_id']) || !in_array($arg_array['flg'], array(0,1)))
            return array( 'code' => 400, 'message' => '参数错误' );

        try {

            //查询文章
            $article_obj = new entity('content_article');
            $article_obj->article_id = $arg_array['article_id'];
            if (!$G_SHOP->DBCA->fetch($article_obj))
                return array( 'code' => 404, 'message' => '文章信息没有找到' );

            if ($arg_array['flg']) {

                if ($article_obj->useFlg == 0) {
                    return array( 'code' => 406, 'message' => '文章已禁用' );
                }
                $article_obj->useFlg = 0;

            } else {

                if ($article_obj->useFlg == 1) {
                    return array( 'code' => 406, 'message' => '文章已启用' );
                }
                $article_obj->useFlg = 1;

            }
            $G_SHOP->DBCA->save($article_obj);

            if ($arg_array['flg'])
                return array( 'code' => 200, 'message' => '文章禁用成功' );
            else
                return array( 'code' => 200, 'message' => '文章启用成功' );

        } catch (Exception $e) {
            if ($e->getCode() > 0) {
                return array( 'code' => $e->getCode(), 'message' => $e->getMessage() );
            } else {
                error_log($e->getMessage());
                return array( 'code' => 500, 'message' => '系统错误' );
            }
        }

    }

    /**
     * 文章分类启用/禁用
     * @param $arg_array
     * @return array
     */
    public function changeArticleCategoryUseflg ( $arg_array ) {

        global $G_SHOP;

        if (!isset($arg_array['category_id'], $arg_array['flg']) || !quanli_is_int($arg_array['category_id']) || !in_array($arg_array['flg'], array(0,1)))
            return array( 'code' => 400, 'message' => '参数错误' );

        try {

            //查询文章分类
            $article_obj = new entity('content_article_category');
            $article_obj->category_id = $arg_array['category_id'];
            if (!$G_SHOP->DBCA->fetch($article_obj))
                return array( 'code' => 404, 'message' => '文章分类信息没有找到' );

            if ($arg_array['flg']) {

                if ($article_obj->useFlg == 0) {
                    return array( 'code' => 406, 'message' => '文章分类已禁用' );
                }
                $article_obj->useFlg = 0;

            } else {

                if ($article_obj->useFlg == 1) {
                    return array( 'code' => 406, 'message' => '文章分类已启用' );
                }
                $article_obj->useFlg = 1;

            }
            $G_SHOP->DBCA->save($article_obj);

            if ($arg_array['flg'])
                return array( 'code' => 200, 'message' => '文章分类禁用成功' );
            else
                return array( 'code' => 200, 'message' => '文章分类启用成功' );

        } catch (Exception $e) {
            if ($e->getCode() > 0) {
                return array( 'code' => $e->getCode(), 'message' => $e->getMessage() );
            } else {
                error_log($e->getMessage());
                return array( 'code' => 500, 'message' => '系统错误' );
            }
        }

    }


}